package com.yang.Filter;

import com.yang.constant.ConfiConstant;
import org.jboss.logging.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CustomerFilter implements Filter {

    private static Logger logger = Logger.getLogger(CustomerFilter.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        String message = (String) request.getSession().getAttribute("message");
        String identification = (String) request.getSession().getAttribute("identfication");
        if(message != null && message.equals("succeed Login") && identification != null && identification.equals(ConfiConstant.ORDINARY_USER)){
            chain.doFilter(servletRequest,servletResponse);
        }else{
            logger.error("未登陆！");
            response.sendRedirect(request.getContextPath()+"/login.html");
        }
    }

    @Override
    public void destroy() {

    }
}
